---
id: extract-with-regex
title: Extract with Regex Node
sidebar_label: Extract with Regex
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

![Extract with Regex Node Screenshot](./assets/extract-with-regex-node.png)

## Overview

The Extract With Regex (or Extract Regex) node allows you to extract one or more strings from a string using a regular expression.

Each capture group in the regular expression will correspond to an output port on the node, so the number of outputs will be equal to the number of capture groups in the regular expression.

The Extract With Regex node can be used for many cases such as parsing a response from an LLM to extract specific values, or parsing a string from a file to extract specific values.

If you do not have any capture groups in your regex, you can still use the `Matches`, `Succeeded`, and `Failed` outputs of the node to determine if the regex matched the input string.

<Tabs
  defaultValue="inputs"
  values={[
    {label: 'Inputs', value: 'inputs'},
    {label: 'Outputs', value: 'outputs'},
    {label: 'Editor Settings', value: 'settings'},
  ]
}>

<TabItem value="inputs">

## Inputs

| Title | Data Type | Description                            | Default Value | Notes                                                                           |
| ----- | --------- | -------------------------------------- | ------------- | ------------------------------------------------------------------------------- |
| Input | `string`  | The string to match the regex against. | (required)    | If the value is not a string, it will be coerced into a string before matching. |

</TabItem>

<TabItem value="outputs">

## Outputs

| Title     | Data Type  | Description                                                                                                                     | Notes                                                  |
| --------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ |
| Output N  | `string`   | Output ports are created, one per capture group in the regular expression path. The value is the contents of the capture group. | Dynamic count based on the number of capture groups.   |
| Matches   | `string[]` | All matches of the regular expression on the string, as an array.                                                               | Contains all matched strings, ignoring capture groups. |
| Succeeded | `boolean`  | Outputs true if the regex was matched on the input string. Outputs false if it was node.                                        |                                                        |
| Failed    | `boolean`  | Outputs true if the regex did not match the input string. Outputs false if it was matched.                                      |                                                        |

</TabItem>

<TabItem value="settings">

## Editor Settings

| Setting         | Description                                                                                                                                                                                                                                        | Default Value                                     | Use Input Toggle | Input Data Type |
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------- | ---------------- | --------------- |
| Error on failed | If true, the Extract With Regex node will error entirely if the input string was not found. This is useful for required matches.                                                                                                                   | False                                             | No               | N/A             |
| Multiline Mode  | If true, the [multiline flag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline) is set on the regular expression, causing `^` and `$` to match the beginning and ends of lines within the string. | False                                             | No               | N/A             |
| Regex           | The regular expression to use for matching against the input string.                                                                                                                                                                               | `([a-zA-Z]+)` (a string of alphabetic characters) | Yes              | `string`        |

</TabItem>

</Tabs>

## Example 1: Extract a command from an LLM response

1. Create a [Chat Node](./chat.mdx) and pass a [Prompt Node](./text.mdx) set to `System` into its `prompt` input, with something like the following:
   ```
   Your reply can initiate commands, for example `!hello` will cause "Hello world" to appear for the user. Try it out now!
   ```
2. Feed the output of the Chat node into an Extract With Regex node with the following regular expression:
   ```
   !([a-zA-Z]+)
   ```
3. Feed the output of the `Output 1` port into another Text node to get the matched command. The output of the Text node should be `hello`.

![Extract with Regex Example 1](./assets/extract-with-regex-node-example-01.png)

## Error Handling

If the input string is not provided, the node will error. If the input is not a string, it will be coerced into a string before matching.

If the regular expression is invalid, the node will error.

If the `Error on failed` setting is enabled, the node will error if the regular expression did not match the input string.

## FAQ

**Q: How do I match a string that contains a special character?**

Special characters such as a newline cannot be escaped in the regular expression editor, however you can put literal newlines in the regular expression in the editor, and they will be preserved. So to match a new line, put a new line in the regular expression editor.

## See Also

- [Extract Object Path Node](./extract-object-path.mdx)
- [Extract JSON Node](./extract-json.mdx)
- [Extract YAML Node](./extract-yaml.mdx)
- [Extract Object Path Node](./extract-object-path.mdx)
- [Match Node](./match.mdx)
